package ru.spb.osll.myfit.common;

import java.util.Comparator;

public class PageExecutingResultComparatorFactory {

    public enum ComparatorType {BY_NAME, BY_TIME, BY_STATE};

    public static Comparator<PageExecutingResult> createComparator(ComparatorType type){
        switch(type){
            case BY_TIME:
                return new TimeComparator();
            case BY_STATE:
                return new StateComparator();
            case BY_NAME:
            default:
                return new NameComparator();
        }
    }

    private static class NameComparator implements Comparator<PageExecutingResult> {
        public int compare(PageExecutingResult o1, PageExecutingResult o2) {
            if (o1 == o2)
                return 0;

            if (o1 != null)
                return (o2 != null) ? o1.getName().compareTo(o2.getName()) : 1;

            return -1;
        }
    }

    private static class TimeComparator implements Comparator<PageExecutingResult> {
        public int compare(PageExecutingResult o1, PageExecutingResult o2) {
            if (o1 == o2)
                return 0;

            if (o1 != null)
                return (o2 != null) ? o1.getStartTime().compareTo(o2.getStartTime()) : 1;

            return -1;
        }
    }


    private static class StateComparator implements Comparator<PageExecutingResult> {
        public int compare(PageExecutingResult o1, PageExecutingResult o2) {
            if (o1 == o2)
                return 0;

            if (o1 != null)
                return (o2 != null) ? o1.getState().compareTo(o2.getState()) : 1;

            return -1;
        }
    }

}